package com.assistant.service.impl;

import com.assistant.common.DateUtil;
import com.assistant.mapper.GetPackageMapper;
import com.assistant.mapper.PostPackageMapper;
import com.assistant.pojo.GetPackage;
import com.assistant.pojo.PostPackage;
import com.assistant.service.GetPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
@Transactional
public class GetPackageServiceImpl implements GetPackageService {
    @Autowired
    private PostPackageMapper postPackageMapper;

    @Autowired
    private GetPackageMapper getPackageMapper;


    public int insertGetPackage(GetPackage getPackage) {
        int a = 0;
        getPackage.setReceive_time(DateUtil.dataFormatUtil(new Date()));
        PostPackage postPackage = new PostPackage();
        postPackage.setPackage_id(getPackage.getPackage_id());
        postPackage.setStatus(1L);
        if (postPackageMapper.updateByPrimaryKeySelective(postPackage) == 1) {
            if (getPackageMapper.insertSelective(getPackage) == 1) {
                a = 1;
            }
        }
        return a;
    }


    public int updateGetPackage(Long id, Integer address_id) {
        GetPackage getPackage = new GetPackage();
        getPackage.setPackage_id(id);
        getPackage.setAddress_id(address_id);
        return getPackageMapper.updateByPrimaryKeySelective(getPackage);
    }


    public int cancelGetPackage(Long id) {
        int a = 0;
        GetPackage getPackage = new GetPackage();
        getPackage.setPackage_id(id);
        getPackage.setStatus(0L);

        PostPackage postPackage = new PostPackage();
        postPackage.setPackage_id(id);
        postPackage.setStatus(0L);
        if (postPackageMapper.updateByPrimaryKeySelective(postPackage) == 1) {
            if (getPackageMapper.updateByPrimaryKeySelective(getPackage) == 1) {
                a = 1;
            }
        }

        return a;
    }


    public int updateFinishedPackage(Long id) {
        int a = 0;
        GetPackage getPackage = new GetPackage();
        PostPackage postPackage = new PostPackage();
        getPackage.setPackage_id(id);
        getPackage.setStatus(2L);
        getPackage.setFinish_time(DateUtil.dataFormatUtil(new Date()));

        postPackage.setPackage_id(id);
        postPackage.setStatus(2L);
        if (getPackageMapper.updateByPrimaryKeySelective(getPackage) == 1) {
            if (postPackageMapper.updateByPrimaryKeySelective(postPackage) == 1) {
                a = 1;
            }
        }
        return a;
    }

}
